package com.training.api.repository;

import com.training.bean.Collect;
import com.training.bean.Course;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface CollectRepository extends JpaRepository<Collect, Integer> {
    // 根据用户ID查找收藏
    @Query("SELECT c FROM Collect c WHERE c.uid = :uid")
    List<Collect> findByUserId(@Param("uid") int uid);

    // 检查特定用户是否收藏了特定内容
    @Query("SELECT COUNT(c) FROM Collect c WHERE c.uid = :uid AND c.cid = :cid")
    int existsByUserIdAndContentId(@Param("uid") int uid, @Param("cid") int cid);

    // 三表联查获取用户收藏的课程详情及学习进度
    @Query("SELECT new com.training.bean.Course(c, col.cid, ltd.study_duration, ltd.last_date) FROM Course c " +
            "JOIN Collect col ON c.id = col.cid " +
            "LEFT JOIN LearningTrackData ltd ON col.uid = ltd.uid AND c.id = ltd.cid " +
            "WHERE col.uid = :uid")
    List<Course> findUserCollectCoursesWithProgress(@Param("uid") int uid);
}